#!/usr/bin/env bash

set -e
set -x

# Ensure we are on the latest commit
# of the branch where we are converting
# recipes from. Currently this is `master`.
export CF_CURRENT_BRANCH="${GITHUB_REF/refs\/heads\//}"
git checkout "${CF_CURRENT_BRANCH}"

# 2 core available on Travis CI Linux workers: https://docs.travis-ci.com/user/ci-environment/#Virtualization-environments
# CPU_COUNT is passed through conda build: https://github.com/conda/conda-build/pull/1149
export CPU_COUNT=2

export PYTHONUNBUFFERED=1

# Install Miniconda.
echo ""
echo "Installing a fresh version of Miniconda."
curl -L https://repo.continuum.io/miniconda/Miniconda3-4.6.14-Linux-x86_64.sh > ~/miniconda.sh
bash ~/miniconda.sh -b -p ~/miniconda
touch ~/miniconda/conda-meta/pinned
(
    source ~/miniconda/bin/activate root

    # Configure conda.
    echo ""
    echo "Configuring conda."
    conda config --set show_channel_urls true
    conda config --set auto_update_conda false
    conda config --set add_pip_as_python_dependency false
    conda config --add channels conda-forge

    unset conda
    conda update -n root --yes --quiet conda
)
source ~/miniconda/bin/activate root

conda install --yes --quiet \
  conda-forge-ci-setup=3.* \
  "conda-smithy>=3.7.1,<4.0.0a0" \
  conda-forge-pinning \
  git=2.12.2 \
  "conda-build>=3.16" \
  "gitpython>=3.0.8" \
  requests \
  ruamel.yaml

conda info
conda config --get

set +x
mkdir -p ~/.conda-smithy
echo $TRAVIS_TOKEN > ~/.conda-smithy/travis.token
echo $PROD_BINSTAR_TOKEN > ~/.conda-smithy/anaconda.token
set -x

python .travis_scripts/create_feedstocks.py
